﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNATools.Engine2D;
using Microsoft.Xna.Framework;
using XNATools.Enums;
using XNATools.Managers;
using XNATools;
using XNATools.Services.Interfaces;
using XNATools.Events;
using XNATools.Events.EventArgs;
using Microsoft.Xna.Framework.Input;

namespace TestLibrairies
{
    public class Bat:Sprite
    {
        #region Fields

        private Vector2 movement = Vector2.Zero;

        #endregion

        #region cTor(s)

        public Bat(Game game)
            : base(game, "Textures\\trampo", Layer.Artwork, 0, false)
        {
            IInputManagerService inputManger = ServiceManager.Get<IInputManagerService>();
            inputManger.KeyboardAction += new KeyboardActionEventHandler(inputManger_KeyboardAction);
        }        

        #endregion

        #region Methods

        public override void Initialize()
        {
            base.Initialize();
            Scale = Helper.ComputeScale(Texture.Width, Texture.Height, 100, 70);
            ForceTransformBounds();
            Position = new Vector2(ScreenManager.ScreenCenter.X, Game.Window.ClientBounds.Height - Bounds.Height);            
        }

        public override void Update(GameTime gameTime)
        {
            OutBorder border;
            if (!IsOutOfBounds(out border, OutOfBoundsBehaviour.TextureHitsBounds))
            {
                Position = Position.Update(movement * gameTime.ElapsedGameTime.Milliseconds);
            }
            base.Update(gameTime);
        }

        private void inputManger_KeyboardAction(object sender, KeyboardActionEventArgs e)
        {
            if (e.KeyboardService.KeyIsJustPressed(Keys.Left))
            {
                movement.X = -0.3f;
            }
            if (e.KeyboardService.KeyIsJustPressed(Keys.Right))
            {
                movement.X = 0.3f;
            }
            if (e.KeyboardService.KeyIsJustReleased(Keys.Left))
            {
                movement.X = 0;
            }
            if (e.KeyboardService.KeyIsJustReleased(Keys.Right))
            {
                movement.X = 0;
            }
        }

        #endregion

        #region Properties
        #endregion
    }
}
